﻿
Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_TiepNhan_Action
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Request.QueryString("Certifiedid") Is Nothing Then
                hidID.Value = Request.QueryString("Certifiedid")
                BindToGrid(hidID.Value)
                'Load list action status
                LoadListActionStatus()
            End If
        End If
        Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
        If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
            ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
        Else
            Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
        End If
    End Sub
    'Show du lieu cua contest
    Sub BindToGrid(ByVal iAnnouncementId As Integer)
        Using data As New VSEntities
            Dim p = (From q In data.CertifiedHouseholds Where q.CertifiedID = iAnnouncementId Select New With {q.CertifiedID,
                                                                                              q.Biz_VietName,
                                                                                              q.Modified,
                                                                                              q.StatusId,
                                                                                            q.Created}).ToList
            Dim strKey_Name() As String = {"CertifiedID", "Biz_VietName", "Modified", "Created", "StatusId"}
            With grdShow
                .DataKeyNames = strKey_Name
                .DataSource = p
                .DataBind()
            End With
        End Using
    End Sub

    Protected Sub grdShow_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShow.RowDataBound
        If e.Row.RowIndex >= 0 Then
            Dim lblModified As Label = CType(e.Row.FindControl("lblModified"), Label)

            Dim strModified As String = Convert.ToDateTime(grdShow.DataKeys(e.Row.RowIndex)("Modified")).ToString("dd/MM/yyyy")
            Dim strCreated As String = Convert.ToDateTime(grdShow.DataKeys(e.Row.RowIndex)("Created")).ToString("dd/MM/yyyy")
            If strModified = "01/01/9000" Or strModified = "01/01/0001" Then
                lblModified.Text = strCreated
            Else
                lblModified.Text = strModified
            End If

            Dim lblTrangThai As Label = CType(e.Row.FindControl("lblTrangThai"), Label)
            Dim _StatusId As Integer = grdShow.DataKeys(e.Row.RowIndex)("StatusId").ToString
            Using data As New VSEntities
                Dim p = (From q In data.Status Where q.StatusId = _StatusId And q.FunctionId = Function_Name.CertifiedHousehold Select q).FirstOrDefault
                If Not p Is Nothing Then
                    lblTrangThai.Text = p.Title
                End If
            End Using
        End If
    End Sub
    Sub LoadListHour(ByVal ddlName As DropDownList)
        For i As Integer = 0 To 23
            Dim iTem As New ListItem(i.ToString(), i.ToString())
            ddlName.Items.Add(iTem)
        Next
    End Sub
    Sub LoadListMinutes(ByVal ddlName As DropDownList)
        For i As Integer = 0 To 59
            Dim iTem As New ListItem(i.ToString(), i.ToString())
            ddlName.Items.Add(iTem)
        Next
    End Sub
    'Load list action status of current status
    'Sub LoadListActionStatus()
    '    Dim iCurrentStatusId As Integer = Request.QueryString("CStatusId")
    '    Using Data As New VSEntities
    '        'Load action tuong ung voi current statusid
    '        Dim st = (From q In Data.StatusActions Where q.CurrentStatusId = iCurrentStatusId Select q Order By
    '                q.NextStatusId).ToList()
    '        For Each i In st
    '            If HasPermission(Function_Name.CertifiedHousehold, Session("RoleID"), i.CurrentStatusId, i.Audit) Then
    '                Dim _item As New ListItem(i.TitleHoSo, i.NextStatusId)
    '                ddlAction.Items.Insert(0, _item)
    '            End If
    '        Next
    '        ddlAction.SelectedIndex = ddlAction.Items.IndexOf(ddlAction.Items.FindByValue(Request.QueryString("NStatusId")))
    '    End Using
    'End Sub

    Sub LoadListActionStatus()


        Using Data As New VSEntities
            hidStatusId.Value = (From q In Data.CertifiedHouseholds Where q.CertifiedID = hidID.Value Select q.StatusId).FirstOrDefault
            'Load action tuong ung voi current statusid & FunctionId
            Dim st = (From q In Data.StatusActions Where q.CurrentStatusId = hidStatusId.Value And q.FunctionId = Function_Name.CertifiedHousehold _
                      Select New With {q.CurrentStatusId, q.NextStatusId, q.Audit, q.ActionId, .ActionTitle = q.Action.Title, .AuditNumber = q.Action.AuditNumber}).ToList()
            For Each i In st
                If HasPermission(Function_Name.CertifiedHousehold, Session("RoleID"), i.CurrentStatusId, i.AuditNumber) Then
                    Dim _item As New ListItem(i.ActionTitle, i.ActionId)
                    ddlAction.Items.Insert(0, _item)
                End If
            Next
            ddlAction.SelectedIndex = ddlAction.Items.IndexOf(ddlAction.Items.FindByValue(Request.QueryString("Actionid")))
            'neu ko co quyen thi redirect
            If ddlAction.Items.Count = 0 Then
                Excute_Javascript("Alertbox('Hồ sơ đã được xử lý.');window.location ='../../Page/TiepNhan/List.aspx';", Me.Page, True)
            End If

        End Using

    End Sub

    'Luu trang thai action cua contest
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim _Certifiedid As Integer = Request.QueryString("Certifiedid")
        Using data As New VSEntities
            Dim p = (From q In data.CertifiedHouseholds Where q.CertifiedID = _Certifiedid Select q).FirstOrDefault
            Try
                p.StatusId = (From q In data.StatusActions Where q.CurrentStatusId = p.StatusId And q.ActionId = ddlAction.SelectedValue _
                              And q.FunctionId = Function_Name.CertifiedHousehold _
                                Select q.NextStatusId).FirstOrDefault

                data.SaveChanges()

                'Ghi vao history changed status
                Dim oHis As New VSModel.ContentActionHistory
                oHis.FunctionId = Function_Name.CertifiedHousehold
                oHis.ObjId = _Certifiedid
                oHis.Creator = Session("Username")
                oHis.Created = Now
                'oHis.PublishedDate = p.PublishedDate 'StringToDateTime(txtPublisheddate.Text.Trim(), ddlbeHour.SelectedValue & ":" & ddlbeMinutes.SelectedValue)
                'oHis.eEffectiveDate = p.eEffectiveDate
                oHis.CurrentStatusId = Request.QueryString("CStatusId")
                oHis.ActionStatusId = ddlAction.SelectedValue
                oHis.Note = txtNote.Text.Trim()
                data.ContentActionHistories.AddObject(oHis)
                data.SaveChanges()
                Insert_App_Log("Changed Status CertifiedHousehold:" & p.Biz_VietName & "", Function_Name.CertifiedHousehold, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Thay đổi trạng trạng thái hồ sơ thành công.');window.location ='../../Page/TiepNhan/Detail.aspx?Certifiedid=" & p.CertifiedID & "';", Me.Page, True)

            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Thay đổi trạng hồ sơ thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub

    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBack.Click
        Response.Redirect("List.aspx")
    End Sub
End Class
